package com.controller;

import com.bean.Customer;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@ResponseBody
public class JsonController {
    @RequestMapping("/testJson")
    public String JsonMethod() {
        Customer cu = new Customer();
        cu.setId(11);
        cu.setUname("class6");
        cu.setPassword("123456");
        cu.setAge(19);
        cu.setEmail("11@163.com");

        Customer cu1 = new Customer();
        cu1.setId(22);
        cu1.setUname("class6-2");
        cu1.setPassword("2222");
        cu1.setAge(20);
        cu1.setEmail("11@163-2.com");

        Customer cu2 = new Customer();
        cu2.setId(33);
        cu2.setUname("class6-3");
        cu2.setPassword("222333");
        cu2.setAge(21);
        cu2.setEmail("11@163-3.com");

        List<Customer> list = new ArrayList<>();
        list.add(cu);
        list.add(cu1);
        list.add(cu2);



        ObjectMapper obj = new ObjectMapper();
        try {
            //返回单个对象Json结构
//            return obj.writeValueAsString(cu);
            return obj.writeValueAsString(list);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;

    }
}
